<?php


namespace App\Services\Game;

use App\DTOS\Admin\GamePackageDTO;
use App\Models\Game\ChannelPackage;
use App\Models\Game\ConfigGroup;
use App\Models\Game\GameChannel;
use App\Models\Game\GamePackage;
use App\Utils\MyLog;
use Illuminate\Support\Facades\DB;

class GamePackageService
{
    public function paginate(GamePackageDTO $DTO){
        $model = GamePackage::query()->with('channels');
        $pageSize = $DTO->getLimit();
        $page = $DTO->getPage();
        $packageNo = $DTO->getPackageNo();
        $type = $DTO->getType();
        $copyright = $DTO->getContent();
        $channelcg = $DTO->getChannelcg();
        if ($channelcg){
            $model->where('channelcg',$channelcg );
        }
        if ($copyright){
            $model->where('content', 'like', '%' . $copyright . '%');
        }
        if ($type){
            $model->where('type',$type);
        }
        if($packageNo){
            $model->where('package_no',$packageNo);
        }
        $model->leftJoin('config_group', 'game_package.channelcg', '=', 'config_group.id')
            ->select('game_package.*', 'config_group.name as config_group_name') // 选择所需字段
            ->orderByDesc('package_no');
        return $model->orderByDesc('package_no')->paginate($pageSize,'*','page',$page);
    }

    /**
     * @param $data ['package_no','version_name','game_channel_id','description']
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function add($data){

        DB::beginTransaction();
        try {
            $insertData = array_only($data,['package_no','type','description','channel','is_test','channelcg','configtype','content','packagechannel','sub_package_no','channeltip']);
            $gameChannelId = data_get($data,'channelcg'); //渠道ID
            if (!$gameChannelId) {
                throw new \Exception("请选择渠道");
            }
            $data['created_at'] = date("Y-m-d H:i:s");
            $gamePackageId = GamePackage::query()->insertGetId($insertData);
            ChannelPackage::query()->where('game_package_id',$gamePackageId)->delete();
            ChannelPackage::query()->insert([
                'game_package_id'=>$gamePackageId,
                'game_channel_id'=>$gameChannelId,
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function edit($gamePackageId,$data){
        DB::beginTransaction();
        try {
            $updateData = array_only($data,['package_no','type','description','channel','is_test','channelcg','configtype','content','packagechannel','sub_package_no','channeltip']);
            MyLog::info('修改的参数11111111111'.json_encode($updateData));
            $gameChannelId = data_get($data,'game_channel_id'); //渠道ID
            $updateData['updated_at'] = date("Y-m-d H:i:s");
            GamePackage::query()->where('id',$gamePackageId)->update($updateData);
            ChannelPackage::query()->where('game_package_id',$gamePackageId)->delete();
            ChannelPackage::query()->insert([
                'game_package_id'=>$gamePackageId,
                'game_channel_id'=>$gameChannelId,
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function delete($gamePackageId){
        DB::beginTransaction();
        try {
            GamePackage::query()->where('id',$gamePackageId)->delete();
            ChannelPackage::query()->where('game_package_id',$gamePackageId)->delete();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    /**
     * @param $gamePackageId
     * @return array
     */
    public function detail($gamePackageId)
    {
        $gamePackage = GamePackage::query()->find($gamePackageId);
        $channelPackage = ChannelPackage::query()->where('game_package_id',$gamePackageId)->first();
        $gameChannelId = data_get($channelPackage,'game_channel_id');
        $gameChannel = GameChannel::query()->find($gameChannelId);
        $gamePackageArray = $gamePackage->toArray();
        $gamePackageArray['gameChannel'] = $gameChannel;
        $gamePackageArray['game_channel_id'] = data_get($gameChannel,'id');
        return $gamePackageArray;
    }



    public function paginatev(GamePackageDTO $DTO){
        $model = GamePackage::query()->with('channels');
        $pageSize = $DTO->getLimit();
        $page = $DTO->getPage();
        $packageNo = $DTO->getPackageNo();
        $type = $DTO->getType();

        if($packageNo){
            $model->where('package_no',$packageNo);
        }
        $model->leftJoin('config_group', 'game_package.channelcg', '=', 'config_group.id')
            ->whereNotNull('game_package.content') // 条件：content 不为 NULL
            ->select('game_package.*', 'config_group.name as config_group_name') // 选择所需字段
            ->orderByDesc('package_no');
        return $model->orderByDesc('package_no')->paginate($pageSize,'*','page',$page);
    }

    /**
     * @param $data ['package_no','version_name','game_channel_id','description']
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function addv($data){

        DB::beginTransaction();
        try {
            $insertData = array_only($data,['package_no','type','description','channel','is_test','channelcg','configtype','copyright','channeltip']);
            $gameChannelId = data_get($data,'channelcg'); //渠道ID
            if (!$gameChannelId) {
                throw new \Exception("请选择渠道");
            }
            $data['created_at'] = date("Y-m-d H:i:s");
            $gamePackageId = GamePackage::query()->insertGetId($insertData);
            ChannelPackage::query()->where('game_package_id',$gamePackageId)->delete();
            ChannelPackage::query()->insert([
                'game_package_id'=>$gamePackageId,
                'game_channel_id'=>$gameChannelId,
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function editv($gamePackageId,$data){
        DB::beginTransaction();
        try {
            $updateData = array_only($data,['package_no','type','description','channel','is_test','channelcg','configtype','copyright','channeltip']);
            MyLog::info('修改的参数'.json_encode($updateData));
            $gameChannelId = data_get($data,'game_channel_id'); //渠道ID
            $data['updated_at'] = date("Y-m-d H:i:s");
            GamePackage::query()->where('id',$gamePackageId)->update($updateData);
            ChannelPackage::query()->where('game_package_id',$gamePackageId)->delete();
            ChannelPackage::query()->insert([
                'game_package_id'=>$gamePackageId,
                'game_channel_id'=>$gameChannelId,
            ]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function deletev($gamePackageId){
        DB::beginTransaction();
        try {
            GamePackage::query()->where('id',$gamePackageId)->delete();
            ChannelPackage::query()->where('game_package_id',$gamePackageId)->delete();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    /**
     * @param $gamePackageId
     * @return array
     */
    public function detailv($gamePackageId)
    {
        $gamePackage = GamePackage::query()->find($gamePackageId);
        $channelPackage = ChannelPackage::query()->where('game_package_id',$gamePackageId)->first();
        $gameChannelId = data_get($channelPackage,'game_channel_id');
        $gameChannel = GameChannel::query()->find($gameChannelId);
        $gamePackageArray = $gamePackage->toArray();
        $gamePackageArray['gameChannel'] = $gameChannel;
        $gamePackageArray['game_channel_id'] = data_get($gameChannel,'id');
        return $gamePackageArray;
    }



}
